﻿using MediatR;
using QuartzServices.Domain.Core.Bus;
using QuartzServices.Domain.Core.Commands;
using QuartzServices.Domain.Core.Events;

namespace QuartzServices.Infrastructure.Bus
{

    /// <summary>
    /// 密封类，实现中介内存总线
    /// </summary>
    public sealed class InMemoryBus : IMediatorHandler
    {
        private readonly IMediator _mediator;

        public InMemoryBus(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 引发事件
        /// </summary>
        /// <typeparam name="T">泛型 继承Event:INotification</typeparam>
        /// <param name="event">事件模型</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task RaiseEvent<T>(T @event) where T : Event
        {
            return _mediator.Publish(@event);
        }

        /// <summary>
        /// 实现我们在IMediatorHandler中定义的接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task SendCommand<T>(T command) where T : Command
        {
            return _mediator.Send(command);
        }
    }
}
